import { deviceTypeMap, partsStatusMap, partTypeMap, markTypeMap, originTypeMap, fixedAssetMap } from '@/config/dict'
import { byKeyGetItem } from '@/utils'
import { validInt } from '@/utils/validate'
import { CustomStatus } from '@/components'

export const searchFormItem = (list) => {
  return [
    {
      type: 'input',
      key: 'keyword',
      label: '部件名称'
    },
    {
      type: 'select',
      key: 'componenTypeId',
      label: '部件类型',
      options: list
    },
    {
      type: 'select',
      key: 'status',
      label: '状态',
      options: partsStatusMap
    },
    {
      type: 'select',
      key: 'fixedAssets',
      label: '是否固定资产',
      labelWidth: '110px',
      options: fixedAssetMap
    }
  ]
}

export const tableColumn = (handleMapShow, handleEdit, handleUpdateStatus, handleDelete) => {
  return [
    {
      key: 'name',
      label: '部件名称',
      minWidth: 140
    },
    {
      key: 'componentType',
      label: '部件类型',
      minWidth: 100,
      render: (_, scope) => byKeyGetItem(partTypeMap, scope.row.componentType).value
    },
    {
      key: 'headerName',
      label: '负责人',
      minWidth: 100
    },
    {
      key: 'coordinateSet',
      label: '位置区域',
      minWidth: 140,
      render: (h, scope) => <el-button size='small' type='text' onClick={() => handleMapShow(scope.row)} >点击查看</el-button>
    },
    {
      key: 'deviceNameStr',
      label: '绑定设备',
      minWidth: 140,
      slotAttrs: {
        options: deviceTypeMap
      }
    },
    {
      key: 'status',
      label: '状态',
      minWidth: 100,
      render: (h, scope) => <CustomStatus list={partsStatusMap} value={scope.row.status} subKey='subKey' />
    },
    {
      type: 'button',
      label: '操作',
      list: [
        {
          name: '修改',
          authKey: 'b_1_2',
          click: handleEdit
        },
        {
          name: (scope) => scope.row.status === 1 ? '停用' : '启用',
          authKey: 'b_1_4',
          click: (id, row) => handleUpdateStatus(id, row.status === 1 ? 2 : 1)
        },
        {
          name: '删除',
          authKey: 'b_1_3',
          confirm: '是否启用?',
          click: handleDelete
        }
      ]
    }
  ]
}

export const modalFormItem = (
  handleComponentTypeChange,
  handleComponentTypeIdChange,
  handleMarkTypeChange,
  handleAnnotation,
  cascaderConfig,
  deviceList,
  dropPartTypeList,
  rediusIsHide,
  parentIdList,
  parentIdShow,
  otherFormItem = []) => {
  return [
    {
      type: 'input',
      key: 'name',
      label: '部件名称',
      required: true,
      col: 12
    },
    {
      type: 'cascader',
      key: 'headerAccountIdList',
      label: '负责人',
      slotAttrs: {
        props: cascaderConfig,
        showAllLevels: false
      },
      required: true,
      col: 12
    },
    {
      type: 'select',
      key: 'componentType',
      label: '部件类别',
      slotAttrs: {
        options: partTypeMap.filter(item => item.key && item.key !== 4)
      },
      slotListeners: {
        change: handleComponentTypeChange
      },
      required: true,
      col: 12
    },
    {
      type: 'select',
      key: 'markType',
      label: '标注方式',
      slotAttrs: {
        options: markTypeMap,
        disabled: true
      },
      slotListeners: {
        change: handleMarkTypeChange
      },
      required: true,
      col: 12
    },
    {
      type: 'select',
      key: 'parentId',
      label: '上级部件',
      isHide: parentIdShow,
      slotAttrs: {
        options: parentIdList
      },
      col: 12
    },
    {
      type: 'input',
      key: 'area',
      label: '面积(平米)',
      slotAttrs: {
        type: 'number'
      },
      rules: [
        { validator: validInt, trigger: 'blur' }
      ],
      col: 12
    },
    {
      type: 'button',
      label: '点击标注',
      slotAttrs: {
        name: '点击设置'
      },
      slotListeners: {
        click: handleAnnotation
      },
      col: 12
    },
    {
      type: 'input',
      key: 'redius',
      label: '半径(米)',
      slotAttrs: {
        type: 'number'
      },
      isHide: rediusIsHide,
      rules: [
        { required: true, message: '半径不能为空' },
        { validator: validInt, trigger: 'blur' }
      ],
      col: 12
    },
    {
      type: 'input',
      key: 'address',
      label: '部件地址',
      slotAttrs: {
        style: { width: '95.8%' }
      },
      required: true,
      col: 24
    },
    {
      type: 'select',
      key: 'deviceIdList',
      label: '绑定设备',
      slotAttrs: {
        options: deviceList,
        multiple: true,
        style: { width: '95.8%' }
      },
      col: 24
    },
    {
      type: 'select',
      key: 'orginType',
      label: '来源',
      slotAttrs: {
        options: originTypeMap
      },
      col: 12
    },
    {
      type: 'input',
      key: 'price',
      label: '价格',
      col: 12
    },
    {
      type: 'select',
      key: 'componentTypeId',
      label: '部件类型',
      slotAttrs: {
        options: dropPartTypeList
      },
      slotListeners: {
        change: handleComponentTypeIdChange
      },
      required: true,
      col: 12
    },
    {
      type: 'input',
      key: 'ownership',
      label: '所有权',
      col: 12
    },
    {
      type: 'radio',
      key: 'status',
      label: '状态',
      slotAttrs: {
        options: partsStatusMap,
        optionKey: { key: 'subKey', value: 'value' }
      },
      required: true,
      col: 12
    },
    {
      type: 'title',
      label: '部件属性',
      otherAttrs: {
        style: { fontSize: '16px', color: '#272727', margin: '0 0  20px 10px', fontWeight: 'bold' }
      },
      col: 24
    },
    ...otherFormItem
  ]
}

export const formButtons = (onSubmit, onReset) => {
  return {
    isDialog: true,
    submitName: '提交',
    resetName: '取消',
    submitCallback: values => onSubmit(values),
    resetCallback: () => onReset()
  }
}
